//
//  LoginViewController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/13.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginNilController.h"
#import "ForgetPWController.h"
#import "RegisterController.h"
#import "MainTabbarController.h"
#import "PosApp-Swift.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *UseriPhone;
@property (weak, nonatomic) IBOutlet UITextField *UserPW;
@property (weak, nonatomic) IBOutlet UIButton *LoginBtn;
@property (strong, nonatomic) MBProgressHUD * hud;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [UIApplication sharedApplication].statusBarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.UseriPhone.keyboardType = UIKeyboardTypeNumberPad;
    self.UseriPhone.delegate = self;
    self.UserPW.delegate = self;
    self.UserPW.secureTextEntry = YES;
    
    [self.LoginBtn setImage:[UIImage imageNamed:@"LoginImage08_None"] forState:UIControlStateNormal];
    self.LoginBtn.layer.cornerRadius = 18;
    self.LoginBtn.layer.shadowColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.75].CGColor;
    self.LoginBtn.layer.shadowOffset = CGSizeMake(0,4);
    self.LoginBtn.layer.shadowOpacity = 1;
    self.LoginBtn.layer.shadowRadius = 8;
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.UseriPhone.text.length > 0 && self.UserPW.text.length > 0) {
        
        [self.LoginBtn setImage:[UIImage imageNamed:@"LoginImage08_Select"] forState:UIControlStateNormal];
        self.LoginBtn.layer.shadowColor = [UIColor colorWithRed:63/255.0 green:203/255.0 blue:255/255.0 alpha:0.75].CGColor;
    }else{
        [self.LoginBtn setImage:[UIImage imageNamed:@"LoginImage08_None"] forState:UIControlStateNormal];
        self.LoginBtn.layer.shadowColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.75].CGColor;
    }
}
- (IBAction)ClickOnBtnToLoginTheApp:(id)sender {
    if (![MainObjectWay IsMobileWithString:self.UseriPhone.text]) {
        [MBProgressHUD showAlertViewWithText:@"请输入正确的手机号"];
        return;
    }
    
    if (self.UserPW.text.length < 6) {
        [MBProgressHUD showAlertViewWithText:@"请填写至少6位密码"];
        return;
    }
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.UseriPhone.text forKey:@"phone"];
    [dic setObject:self.UserPW.text forKey:@"password"];
    
    self.hud = [MainObjectWay CreateHUDView:self.view];
    
    [MainObjectWay PostWithMethod:@"index" andAction:@"Login" andAddParam:YES parameters:dic success:^(id responseObject) {
        
        NSLog(@"登陆接口请求的数据 = %@",responseObject);
        NSLog(@"登入提示= %@",responseObject[@"msg"]);
        [self.hud hideAnimated:YES];
        if ([MainObjectWay IsSucceedWithCode:responseObject[@"code"]]) {
            NSMutableDictionary * dic = responseObject[@"data"];
            NSString * iPhoneStr = [NSString stringWithFormat:@"%@",dic[@"phone"]];
            NSString * IDStr = [NSString stringWithFormat:@"%@",dic[@"member_id"]];
            NSString * str = [NSString stringWithFormat:@"%@",dic[@"salt"]];
            
            AppContext.shared.member_id = [NSString stringWithFormat:@"%@",dic[@"member_id"]];
            AppContext.shared.merchant = [NSString stringWithFormat:@"%@",dic[@"merchant"]];
            AppContext.shared.access_token = [NSString stringWithFormat:@"%@",dic[@"access_token"]];

            [AppContext.shared synchronizeLoginInfo];
            
            SetKLogin(YES);
            SetKUseriPhone(iPhoneStr);
            SetKUserID(IDStr);
            SetKAliStr(str);
            
            
            
            UIApplication *app = [UIApplication sharedApplication];
            AppDelegate *dele = (AppDelegate*)app.delegate;
            dele.window.rootViewController = [[MainTabbarController alloc] init];
            
//            NSString * RegisterID = [JPUSHService registrationID];
//            NSMutableDictionary * JPDic = [[NSMutableDictionary alloc] init];
//            [JPDic setObject:RegisterID forKey:@"register_code"];
//            [JPDic setObject:KUseriPhone forKey:@"phone"];
//            
//            [MainObjectWay PostWithMethod:@"push_register" andAction:@"User" andAddParam:YES parameters:JPDic success:^(id responseObject) {
//                NSLog(@"成功信息 = %@",responseObject);
//            } failure:^(NSError *error) {
//                NSLog(@"错误信息 = %@",error);
//            }];
            
        }else{
            [MBProgressHUD showAlertViewWithText:responseObject[@"msg"]];
            NSLog(@"++++%@",responseObject[@"msg"]);
        }
    } failure:^(NSError *error) {
        NSLog(@"错误原因 = %@",error);
        [self.hud hideAnimated:YES];
    }];
}

- (IBAction)ClickOnBtnToRegisterTheApp:(id)sender {
    RegisterController * RegisterC = [[RegisterController alloc] init];
    [self.navigationController pushViewController:RegisterC animated:YES];
}
- (IBAction)ClickOnBtnToChangePassWord:(id)sender {
    
    ForgetPWController * PWC = [[ForgetPWController alloc] init];
    PWC.title = @"找回密码";
    [self.navigationController pushViewController:PWC animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
